package com.QSystem.controller;


import com.QSystem.mapper.TitleMapper;
import com.QSystem.pojo.Survey;
import com.QSystem.mapper.SurveyMapper;
import com.QSystem.pojo.User;
import com.QSystem.pojo.WholeAnswer;
import com.QSystem.pojo.WholeSurvey;
import com.QSystem.service.AnswerService;
import com.QSystem.service.UserService;
import com.QSystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    SurveyMapper surveyMapper;
    @Autowired
    UserService userService;
    @Autowired
    AnswerService answerService;

    Map<String,Object> map=new HashMap<>();

    //根据surid获取survey
    @GetMapping(value = "/{surId}")
    public Result getSurvey(@PathVariable Integer surId){
        System.out.println(surId);
        //根据传过来的参数获取对应id的survey
        return answerService.getSurvey(surId);
    }

    //存储用户的答卷（答卷数要加一）
    @PostMapping(value = {"/saveAnswer"},produces = {"application/json; charset=UTF-8"})
    public Result saveAnswer(@RequestBody WholeAnswer wholeAnswer){
        System.out.println(wholeAnswer);
       return answerService.addAnswer(wholeAnswer);

    }


    //根据surId获取答题情况
    @PostMapping(value = {"/getAnswer"},produces = {"application/json; charset=UTF-8"})
    public Result getAnswer(@RequestBody Integer surId){
        //System.out.println(wholeAnswer);
        return answerService.getAnswer(surId);

    }

    @PostMapping(value = {"/gettitle"},produces = {"application/json; charset=UTF-8"})
    public Result getTitle(@RequestBody Integer surId){
        System.out.println(surId);
        return answerService.gettitle(surId);

    }

//    @GetMapping("/getallsurvey")
//    public List<Survey> getAllServy(){
//        List<Survey> allSurvey = surveyMapper.getAllSurvey();
//        for (Survey survey : allSurvey) {
//            System.out.println(survey);
//        }
//        return allSurvey;
//    }
//    @GetMapping("/query")
//    public User query(@RequestParam("name") String name){
//        System.out.println(name);
//        return userService.query(new User(1,"xiaoming","123456"));
//
//    }
//    @PostMapping(value = {"/post"},produces = {"application/json; charset=UTF-8"})
//    public Map test(@RequestBody String body){
//        map.put("body",body);
//        System.out.println(body);
//
//        return map;
//    }
//    @GetMapping("/get")
//    public User get(){
//        return userService.query(new User(6,"321","321"));
//    }
//    @GetMapping("/delete")
//    public void delete(){
//        userService.deleteUser(new User(1,"11","111"));
//    }
//    @GetMapping("/update")
//    public void update(){
//        userService.updateUser(new User(3,"11","111"));
//    }
//    @GetMapping("/add")
//    public void add(){
//        userService.addUser(new User(7,"11","11421311"));
//    }
//
//    @GetMapping("/test")
//    public void test(){
//        userService.getUserId("admin");
//    }
}
